package es.udc.pfc.davidparedes.web.pages.user;

import java.util.List;

import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;

import es.udc.pfc.davidparedes.model.userprofile.UserProfile;
import es.udc.pfc.davidparedes.model.userservice.UserService;
import es.udc.pfc.davidparedes.model.userservice.VendorRowStatsDTO;
import es.udc.pfc.davidparedes.model.userservice.VendorUniverseStatsDTO;
import es.udc.pfc.davidparedes.model.util.GlobalNames;
import es.udc.pfc.davidparedes.model.util.InstanceNotFoundException;
import es.udc.pfc.davidparedes.web.services.AuthenticationPolicy;
import es.udc.pfc.davidparedes.web.services.AuthenticationPolicyType;
import es.udc.pfc.davidparedes.web.util.UserSession;

@AuthenticationPolicy(AuthenticationPolicyType.AUTHENTICATED_USERS)
public class Home {
	
	@Property
	private UserProfile userProfile;
	
	@Property
	private String weekHours;
	
	@Property
	private String monthHours;
	
	@Property
	private String trimesterHours;
	
	@Property
	private List<VendorUniverseStatsDTO> tasksStats;
	
	@Property
	private VendorUniverseStatsDTO uniStats;
	
	@Property
	private List<VendorRowStatsDTO> missionsStats;
	
	@Property
	private VendorRowStatsDTO missionRow;
	
	@Property
	private VendorRowStatsDTO taskRow;
	
	@Property
	private Long weekTotalMissions;
	
	@Property
	private Long monthTotalMissions;
	
	@Property
	private Long trimesterTotalMissions;
	
	@Property
	private Long weekTotalTasks;
	
	@Property
	private Long monthTotalTasks;
	
	@Property
	private Long trimesterTotalTasks;
	
	@Property
	private Long weekTotalHours;
	
	@Property
	private Long monthTotalHours;
	
	@Property
	private Long trimesterTotalHours;
    
    @SessionState(create=false)
    private UserSession userSession;
    
    @Inject
    private UserService userService;
    
    @Inject
    private Messages messages;
    
    void onActivate() {
    	try {
			userProfile = userService.findUserProfile(userSession.getUserProfileId());
			if (isManager()) {
				Long[] stats = userService.getManagerStats(userProfile.getUserProfileId());
				weekHours = formatTime(stats[0]);
				monthHours = formatTime(stats[1]);
				trimesterHours = formatTime(stats[2]);
			} else {
				if (isVendor()) {
					// Tasks
					tasksStats = userService.getVendorTasksStats(userProfile.getUserProfileId());
					weekTotalTasks = (long) 0;
					monthTotalTasks = (long) 0;
					trimesterTotalTasks = (long) 0;
					for (VendorUniverseStatsDTO uni : tasksStats) {
						for (VendorRowStatsDTO row : uni.getRows()) {
							weekTotalTasks += row.getWeekMillis();
							monthTotalTasks += row.getMonthMillis();
							trimesterTotalTasks += row.getTrimesterMillis();
						}
					}
					
					// Missions
					missionsStats = userService.getVendorMissionsStats(userProfile.getUserProfileId());
					weekTotalMissions = (long) 0;
					monthTotalMissions = (long) 0;
					trimesterTotalMissions = (long) 0;
					for (VendorRowStatsDTO row : missionsStats) {
						weekTotalMissions += row.getWeekMillis();
						monthTotalMissions += row.getMonthMillis();
						trimesterTotalMissions += row.getTrimesterMillis();
					}
					
					// Total
					weekTotalHours = weekTotalTasks + weekTotalMissions;
					monthTotalHours = monthTotalTasks + monthTotalMissions;
					trimesterTotalHours = trimesterTotalTasks + trimesterTotalMissions;
				}
			}
		} catch (InstanceNotFoundException e) {
		}
    }
    
    public boolean hasStats() {
    	if ((userSession.getUserType().equals(GlobalNames.USER_TYPE_VENDOR))
    	| (userSession.getUserType().equals(GlobalNames.USER_TYPE_UNIVERSE_MANAGER))) {
   			return true;
   		}
        return false;
	}
    
    public boolean isVendor() {
   		if (userSession.getUserType().equals(GlobalNames.USER_TYPE_VENDOR)) {
   			return true;
   		}
        return false;
	}
   	
   	public boolean isManager() {
   		if (userSession.getUserType().equals(GlobalNames.USER_TYPE_UNIVERSE_MANAGER)) {
   			return true;
   		}
        return false;
	}
   	
   	public String formatTime(Long n) {
   		Integer h = (int) (n / (1000*60*60));
   		Integer m = (int) ((n % (1000*60*60)) / (1000*60));
   		String text = h + " " + messages.get("hours");
   		if (m > 0) {
   			text += ", " + m + " " + messages.get("minutes");
   		}
   		return text;
   	}
   	
   	public Boolean isNotEmpty(List<Object> l) {
   		if (l.size()>0) {
   			return true;
   		}
   		return false;
   	}

}
